import 'package:flutter/material.dart';
import 'package:star_author/utils/utils.dart';

class SwiperViewWidget extends StatefulWidget {
  final int init;
  final Axis direction;
  final bool keep, disabled, auto;
  final int duration;
  final GlobalKey? tabKey;
  final Function(int)? onChange;
  late List<Widget> children;
  late IndexedWidgetBuilder itemBuilder;
  int? itemCount;

  late bool isBuilder;

  SwiperViewWidget({
    Key? key,
    this.init = 0,
    this.direction = Axis.horizontal,
    this.keep = false,
    this.disabled = false,
    this.auto = false,
    this.duration = 4000,
    this.tabKey,
    this.onChange,
    required this.children,
  })  : isBuilder = false,
        super(key: key);

  SwiperViewWidget.builder({
    Key? key,
    this.init = 0,
    this.direction = Axis.horizontal,
    this.keep = false,
    this.disabled = false,
    this.auto = false,
    this.duration = 4000,
    this.tabKey,
    this.onChange,
    required this.itemBuilder,
    this.itemCount,
  })  : isBuilder = true,
        super(key: key);

  _SwiperViewWidgetState createState() => _SwiperViewWidgetState();
}

class _SwiperViewWidgetState extends State<SwiperViewWidget> {
  late PageController _controller;
  late String? autoKey;

  @override
  initState() {
    super.initState();
    _controller = PageController(
      initialPage: widget.init,
      keepPage: widget.keep,
    );

    if (widget.auto) {
      autoKey = GetTimer.add(
        name: 'swiper',
        millisec: widget.duration,
        callback: (timer) {
          if (!widget.isBuilder) {
            if (_controller.page!.toInt() < widget.children.length - 1) {
              _controller.nextPage(
                duration: Duration(milliseconds: 800),
                curve: Curves.easeInOut,
              );
            } else {
              animateTo(0);
            }
          } else {
            _controller.nextPage(
              duration: Duration(milliseconds: 800),
              curve: Curves.easeInOut,
            );
          }
        },
      );
    }
  }

  @override
  void dispose() {
    if (widget.auto) {
      GetTimer.remove(name: 'swiper', key: autoKey!);
    }
    _controller.dispose();
    super.dispose();
  }

  void onPageChanged(int index) {
    if (widget.tabKey != null) {
      dynamic _state = widget.tabKey!.currentState;
      _state.setActive?.call(index);
    }
    widget.onChange?.call(index);
  }

  void animateTo(int index) {
    _controller.jumpToPage(index);
  }

  Widget build(BuildContext ctx) {
    return widget.isBuilder
        ? PageView.builder(
            controller: _controller,
            scrollDirection: widget.direction,
            onPageChanged: onPageChanged,
            physics:
                widget.disabled ? new NeverScrollableScrollPhysics() : null,
            itemBuilder: widget.itemBuilder,
            itemCount: widget.itemCount,
          )
        : PageView(
            controller: _controller,
            scrollDirection: widget.direction,
            onPageChanged: onPageChanged,
            physics:
                widget.disabled ? new NeverScrollableScrollPhysics() : null,
            children: widget.children,
          );
  }
}
